package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivitySetEmergencyContactBinding
import com.haoshuang.zhouzhoubang.ui.vm.SetEmergencyContactViewModel
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.CountDownWorker
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.visibleOrGone


/**
 *  修改紧急手机号
 */
class SetEmergencyContactActivity :
    BaseVMActivity<SetEmergencyContactViewModel, ActivitySetEmergencyContactBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<SetEmergencyContactActivity>()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "备用手机号"
        binding.titleBar.leftImageView().click { finish() }

        val user = AccountUtils.user
        binding.tvOldPhone.text = user?.spare_phone

        binding.llOldPhone.visibleOrGone(user?.spare_phone?.isNotEmpty() == true)

        binding.tvGetCode.clickWithTrigger {
            // 点击获取短信验证码
            val mobile = binding.etPhone.text.toString()
            if (mobile.isEmpty()) {
                toast("请输入紧急手机号")
                return@clickWithTrigger
            }
            if (mobile == user?.phone) {
                toast("紧急手机号不能和原手机号一致")
                return@clickWithTrigger
            }
            if (mobile == user?.spare_phone) {
                toast("新紧急手机号不能和原紧急手机号一致")
                return@clickWithTrigger
            }
            binding.tvGetCode.isEnabled = false
            vm.sendSms(mobile)
        }

        binding.tvSave.clickWithTrigger{
            val mobile = binding.etPhone.text.toString()
            if (mobile.isEmpty()) {
                toast("请输入紧急手机号")
                return@clickWithTrigger
            }
            if (mobile == user?.phone) {
                toast("紧急手机号不能和原手机号一致")
                return@clickWithTrigger
            }
            if (mobile == user?.spare_phone) {
                toast("新紧急手机号不能和原紧急手机号一致")
                return@clickWithTrigger
            }
            val code = binding.etCode.text.toString()
            if (code.isEmpty()){
                toast("请输入验证码")
                return@clickWithTrigger
            }
            vm.editEmergencyContact(mobile, code)
        }

    }

    override fun initData() {

    }

    override fun startObserve() {

        vm.editEmergencyContactData.observeState(this){
            onLoading{showLoading()}
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess{
                dismissLoading()
                val user = AccountUtils.user
                user?.spare_phone = binding.etPhone.text.toString()
                AccountUtils.user = user
                setResult(200)
                finish()
            }
        }

        vm.sendSmsData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.tvGetCode.isEnabled = true
            }
            onSuccess {
                dismissLoading()
                CountDownWorker(
                    this@SetEmergencyContactActivity,
                    total = 60,
                    step = 1,
                    onChange = {
                        binding.tvGetCode.text = "$it"
                    },
                    onFinish = {
                        binding.tvGetCode.text = "重新获取"
                        binding.tvGetCode.isEnabled = true
                    }).start()
            }
        }

    }

}